﻿using Pay.DTO;
using Pay.IService;
using Pay.Service;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Http.Description;
using Flurl.Http;
using Pay.Core;

namespace Pay.Token.Controllers {
    /// <summary>
    ///通用
    /// </summary>
    [RoutePrefix("common")]
    public class CommonController : BaseController {
        private ISettingService _iSettingService;
        private IMerchantService _iMerchantService;
        private IRegionService _iRegionService;
        public CommonController(ISettingService iSettingService, IMerchantService iMerchantService, IRegionService iRegionService) {
            _iSettingService = iSettingService;
            _iMerchantService = iMerchantService;
            _iRegionService = iRegionService;
        }
        #region 发验证码短信
        /// <summary>
        /// 发验证码短信:原token_login 注册 token_register 短信登录 token_login_code 忘记密码 token_forget 新增修改银行卡 token_bank
        /// </summary>
        /// <param name="send_model">发送短信请求信息</param>
        /// <returns></returns>
        [Route("regsms"), HttpPost, AllowAnonymous]
        public ResultMessage<bool> Regsms([FromBody]send_model send_model) {
            if (!ModelState.IsValid)
                return ResultToJson.Json(ModelError, false);
            if (send_model.use_type == "token_bank") {
                if (UserId == 0) throw new ServerErrorException("您还未登录");
                var user = _iMerchantService.Get(UserId);
                send_model.mobile = user.mobile;
                send_model.mobile_code = user.mobile_code;
            }
            var result = _iSettingService.SendTokenRegsms(send_model, ESMSVerificationCodeSource.APP);
            if (result == "0")
                return ResultToJson.Json("接收成功", true);
            return ResultToJson.Json<bool>(result);
        }
        #endregion
        /// <summary>
        /// 查询全部省市区县
        /// </summary>
        /// <returns></returns>
        [Route("regions"), HttpGet, AllowAnonymous]
        public ResultMessage<IEnumerable<region_dto>> Region() {
            var list = _iRegionService.GetAll(0, 3, false);
            return ResultToJson.Json("接收成功", list);
        }
        /// <summary>
        /// 商城路径
        /// </summary>
        /// <returns></returns>
        [Route("mall_path"), HttpGet, AllowAnonymous]
        public ResultMessage<List<string>> TokenMallPath() {
            List<string> list = new List<string>();
            list.AddRange(new string[] { "distributionmall", "memberspace", "mybill", "myorder", "order", "productmanage", "producttokenmaker", "shopInfo", "topic" });
            return ResultToJson.Json("接收成功", list);
        }
        #region 版本
        /// <summary>
        /// 相关版本
        /// </summary>
        /// <returns>The version.</returns>
        [Route("version"), HttpGet, AllowAnonymous]
        public ResultMessage<token_app_setting> Version() {
            var result = _iSettingService.GetTokenSetting().app;
            return ResultToJson.Json("获取成功", result);
        }
        /// <summary>
        /// 提现配置信息
        /// </summary>
        /// <returns></returns>
        [Route("transfer_base"), HttpGet, AllowAnonymous]
        public ResultMessage<token_base_setting> TransferBaseInfo() {
            var result = _iSettingService.GetTokenSetting().token_base_setting;
            return ResultToJson.Json("获取成功", result);
        }

        #endregion

        #region soo的token
        /// <summary>
        /// 请求OSS的STS临时TOKEN
        /// </summary>
        /// <returns>The oken.</returns>
        [Route("sts_token"), HttpGet]
        public async Task<ResultMessage<t_sts_token_ex>> OSSToken() {
            var data = new t_head_req();
            data.method = "1001";
            var result = await ChannelApi.Current.PostApi<t_sts_token>(data);
            if (result.result_code == 1000)
                return ResultToJson.Json("获取成功", new t_sts_token_ex {
                    accesskey_id = result.accesskey_id,
                    accesskey_secret = result.accesskey_secret,
                    expiration = result.expiration,
                    token = result.token
                });
            return ResultToJson.Json<t_sts_token_ex>("获取失败");
        }
        #endregion
        #region caipiao
        /// <summary>
        /// 彩票接口
        /// </summary>
        /// <returns>The piao.</returns>
        /// <param name="mobile">Mobile.</param>
        [Route("caipiao"), HttpGet]
        public async Task<ResultMessage<caipiao_res>> CaiPiao(string mobile) {
            if (string.IsNullOrEmpty(mobile))
                return ResultToJson.Json<caipiao_res>("手机号必填");
            var log = new LogHelper("info");
            try {
                var content = new Dictionary<string, string>();
                content.Add("appId", RequestMethod.CAI_PIAO_APPID);
                content.Add("accessToken", SecureHelper.MD5(RequestMethod.CAI_PIAO_APPID + RequestMethod.CAI_PIAO_SECRET_KEY));
                content.Add("userTel", mobile);
                var formUrlContent = new FormUrlEncodedContent(content);
                var resultApi = await RequestMethod.CAI_PIAO_URL.PostAsync(formUrlContent).ReceiveString();
                log.Info(resultApi);
                var result = resultApi.ToObject<caipiao_res>();
                if (result.code == 600)
                    return ResultToJson.Json(result);
                else
                    return ResultToJson.Json<caipiao_res>(result.msg);
            } catch (FlurlHttpException ex) {
                log.Info(ex.Message);
            }

            return ResultToJson.Json<caipiao_res>("获取失败");
        }
        #endregion
       
    }

}
